package oj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LC47 {
    List<List<Integer>> list=new ArrayList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<Integer> path=new ArrayList<>();
        Arrays.sort(nums);
        boolean[] flag=new boolean[nums.length];
        dfs(nums,path,0,flag);
        return list;
    }
    private void dfs(int[] nums,List<Integer> path,int depth,boolean[] flag){
        if(depth==nums.length){
            list.add(new ArrayList<>(path));
            return;
        }

        for(int i=0;i<nums.length;i++){
            if(flag[i]){
                continue;
            }
            if(i>0 && nums[i]==nums[i-1] && !flag[i-1]){
                continue;
            }
            path.add(nums[i]);
            flag[i]=true;
            dfs(nums,path,depth+1,flag);
            flag[i]=false;
            path.remove(path.size()-1);

        }
    }
}
